
import json
import requests

def get_training_data():
    url = "http://127.0.0.1:5000/api/v0/get_training_data"
    # 发送POST请求
    response = requests.get(url)
    # 检查响应状态码
    if response.status_code == 200:
        # 打印响应内容
        print("Response:")
        print(response.text)
    else:
        print("Error:", response.status_code)
    return response.text

def main():
    try:
        res = get_training_data()
        json_data = json.loads(res)["df"]
        data = json.loads(json_data)

        # 创建一个空列表来存储转换后的数据
        transformed_data = []
        # 遍历数组中的每个元素
        for item in data:
            # 提取并打印每个元素的'question'和'content'字段
            question = item.get('question', '')  # 如果没有'question'字段，则返回默认值
            content = item.get('content', '')  # 如果没有'content'字段，则返回默认值
            # 创建一个新的字典并添加到列表中
            if question != '' and question is not None:
                transformed_data.append({"question": question, "sql": content})
            print('Question:', question)
            print('Content:', content)
            print('---')

        # 将转换后的数据写入到文件中
        with open('./data/smartpole_sql.json', 'w', encoding='utf-8') as f:
            json.dump(transformed_data, f, ensure_ascii=False, indent=4)
    except FileNotFoundError:
        print("Text file not found.")


if __name__ == "__main__":
    main()

